/**
 * 博客路由
 */

const querystring = require('querystring')
const { getList, getDetail, createBlog, editBlog, deleteBlog } = require('../controller/blog')
const { SuccessModel, ErrorModel } = require('../model/resModel')
const getCurrentTime = require('../util/getCurrentTime')

const loginCheck = (req) => {
    if (!req.session.username) {
        return Promise.resolve(
            new ErrorModel({
                tip: '您尚未登录，无权限操作',
                failTime: getCurrentTime()
            }, 'fail')
        )
    }
}

const blogRouterHandle = (req, res) => {
    // 解析req
    const path = req.url.split('?')[0]
    const method = req.method
    const getStr = req.url.split('?')[1]
    // 解析getStr
    const getData = querystring.parse(getStr)
    // 解析postData
    const postData = req.body
    // 博客列表
    if (method === 'GET' && path === '/api/blog/list') {
        if (loginCheck(req)) {
            return loginCheck(req)
        }
        const author = getData.author
        const keyword = getData.keyword
        const promise = getList(author, keyword)
        return promise.then(sqlData => {
            return new SuccessModel(sqlData, 'ok')
        })
    }
    // 博客详情
    if (method === 'GET' && path === '/api/blog/detail') {
        if (loginCheck(req)) {
            return loginCheck(req)
        }
        const id = getData.id
        const promise = getDetail(id)
        return promise.then(sqlData => {
            return new SuccessModel(sqlData[0], 'ok')
        })
    }
    // 新建博客
    if (method === 'POST' && path === '/api/blog/create') {
        const title = postData.title
        const content = postData.content
        const author = postData.author
        const currentTime = getCurrentTime()
        const promise = createBlog(title, content, author, currentTime, currentTime)
        return promise.then(sqlData => {
            if (sqlData.affectedRows && sqlData.insertId) {
                return new SuccessModel({
                    tip: '新建成功，id为：' + sqlData.insertId,
                    createTime: currentTime
                }, 'ok')
            } else {
                return new ErrorModel({
                    tip: '新建失败',
                    failTime: currentTime
                }, 'fail')
            }
        })
    }
    // 编辑博客
    if (method === 'POST' && path === '/api/blog/edit') {
        if (loginCheck(req)) {
            return loginCheck(req)
        }
        const title = postData.title
        const content = postData.content
        const author = postData.author
        const currentTime = getCurrentTime()
        const id = getData.id
        const promise = editBlog(title, content, author, currentTime, id)
        return promise.then(sqlData => {
            if (sqlData.affectedRows) {
                return new SuccessModel({
                    tip: '修改成功',
                    updateTime: currentTime
                }, 'ok')
            } else {
                return new SuccessModel({
                    tip: '修改失败',
                    failTime: currentTime
                }, 'fail')
            }
        })
    }
    // 删除博客
    if (method === 'POST' && path === '/api/blog/delete') {
        if (loginCheck(req)) {
            return loginCheck(req)
        }
        const id = getData.id
        const currentTime = getCurrentTime()
        const promise = deleteBlog(id)
        return promise.then(sqlData => {
            if (sqlData.affectedRows) {
                return new SuccessModel({
                    tip: '删除成功',
                    deleteTime: currentTime
                }, 'ok')
            } else {
                return new SuccessModel({
                    tip: '删除失败',
                    failTime: currentTime
                }, 'fail')
            }
        })
    }
}

module.exports = blogRouterHandle